package ua.pp.bizon.yandex.uploader;

import ua.pp.bizon.yandex.api.IJobFactory;
import ua.pp.bizon.yandex.api.ObjectLookup;
import ua.pp.bizon.yandex.api.QueryMBean;
import ua.pp.bizon.yandex.jmx.IJMXService;

import javax.management.MalformedObjectNameException;

public class MainApp {

	public static final String HTTP_API_FOTKI_USERS_PATH = "api/users/";
	public static final String HTTP_API_FOTKI_YANDEX_RU = "http://api-fotki.yandex.ru/";

	private static MainApp app;

	public static void main(String args[]) throws MalformedObjectNameException {
		boolean relogin = false;
		boolean asService = false;
		boolean connect = false;
		String name = "";
		for (String arg : args) {
			if (arg.startsWith("--login=")) {
				relogin = true;
				name = arg.substring(8);
			}
			if (arg.startsWith("--relogin")) {
				relogin = true;
				name = "";
			}
			if (arg.equals("--service")) {
				asService = true;
			}
			if (arg.equals("--connect")) {
				connect = true;
			}
		}
		app = new MainApp();
		app.start(asService, connect);
		if (relogin) {
			ObjectLookup.lookup(IJMXService.class).getQuery().addActivity(ObjectLookup.lookup(IJobFactory.class).relogin(name));
		}
	}

	protected void start(boolean asService, boolean connect) throws MalformedObjectNameException {
		if (ObjectLookup.lookup(IJMXService.class).init(asService, connect)) {
			QueryMBean queryMBean = ObjectLookup.lookup(IJMXService.class).getQuery();
			if (connect) {
				System.out.println(queryMBean.getActivities());
			} else if (!asService) {
				queryMBean.addActivity(ObjectLookup.lookup(IJobFactory.class).StartGUI());
			}
		}
	}
}
